#include "loadfrommerriam.h"
#include "ui_loadfrommerriam.h"

#include <QTextStream>
#include <QDebug>
#include <QWebFrame>
#include <QSqlError>
#include <QSqlDriver>
#include <QSqlField>
#include <QWebHistory>

LoadFromMerriam::LoadFromMerriam(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LoadFromMerriam)
{
    ui->setupUi(this);
    //createDBConnection();    
    m_file.setFileName("/home/florina/work/Dictionary/trunk/translations/words_merriam.xml");
    /*open a file */
    if (!m_file.open(QIODevice::WriteOnly))
    {
        qDebug()<<"Can't open file";
    }
    else
    {
        /*if file is successfully opened then create XML*/
        m_xmlWriter = new QXmlStreamWriter();
        /* set device (here file)to streamwriter */
        m_xmlWriter->setDevice(&m_file);
        /* Writes a document start with the XML version number version. */
        m_xmlWriter->writeStartDocument();
        m_xmlWriter->writeStartElement("words");
    }
}

LoadFromMerriam::~LoadFromMerriam()
{
    delete ui;
    if(m_xmlWriter != NULL)
    {
        m_xmlWriter->writeEndElement();
        /*end document */
        m_xmlWriter->writeEndDocument();
       // delete m_xmlWriter;
    }
}

void LoadFromMerriam::LoadFormTxt()
{
    //QFile file("/home/florina/work/trunk/translations/words.txt");
    QFile file("/home/florina/work/Dictionary/trunk/translations/words.txt");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
       qDebug()<<"Can't open file";
        return;
    }
    QTextStream in(&file);
    QString word = in.readLine();
    while(!in.atEnd())
    {
        m_words.append(word);
        word = in.readLine();
    }
}

void LoadFromMerriam::loadPage(int wordIndex)
{
    m_wordLoad = m_words.at(wordIndex);
    QString  whatGet = "http://www.merriam-webster.com/dictionary/"+m_wordLoad;
    //QString  whatGet = "http://www.merriam-webster.com/dictionary/irony";    
    ui->webView->stop();
    QWebHistory *history = ui->webView->page()->history();
    if(history)
        history->clear();

    QWebPage *page = new QWebPage(ui->webView);
    ui->webView->setPage(page);
    ui->webView->page()->mainFrame()->setUrl(whatGet);
    //ui->webView->setUrl(QUrl(whatGet));
    //ui->webView->load(QUrl(whatGet));
}
void  LoadFromMerriam::initPage()
{
    connect(ui->webView->page()->currentFrame (), SIGNAL(loadFinished(bool)), this, SLOT(parsePage(bool)));
    m_wordIndex = 0;
    ui->progressBar->setRange(2000, 10000);   
    loadPage(m_wordIndex);
}

void LoadFromMerriam::removeImg(QString &definition)
{
    QString img       = "img ";
    int start_idx_img = definition.indexOf(img);
    int end_idx_img   = definition.indexOf(">", start_idx_img+1);
    definition        = definition.remove(start_idx_img, end_idx_img-start_idx_img);

    if( definition.indexOf(img) != -1)
        removeImg(definition);
}

void LoadFromMerriam::removeBackground(QString &definition)
{
    QString back       = "background: url(/images/lma_thes.jpg)";
    int start_idx_back = definition.indexOf(back);
    int end_idx_back   = definition.indexOf(";", start_idx_back+1);
    definition         = definition.remove(start_idx_back, end_idx_back-start_idx_back);

    back           = "background: url(/images/lma_span.jpg)";
    start_idx_back = definition.indexOf(back);
    end_idx_back   = definition.indexOf(";", start_idx_back+1);
    definition     = definition.remove(start_idx_back, end_idx_back-start_idx_back);

    back           = "background: url(/images/lma_enc.jpg)";
    start_idx_back = definition.indexOf(back);
    end_idx_back   = definition.indexOf(";", start_idx_back+1);
    definition     = definition.remove(start_idx_back, end_idx_back-start_idx_back);
}

void LoadFromMerriam::removeLinks(QString &html)
{
    QString keyLink = "<a href=";
    int idxStart = -1;
    int idxEnd = -1;
    idxStart = html.indexOf(keyLink);
    while(idxStart > -1)
    {
        idxEnd = html.indexOf(">", idxStart+1);
        html.remove(idxStart, idxEnd - idxStart+1);
        idxStart = html.indexOf(keyLink);
    }
}

void LoadFromMerriam::removeWord(QString &html, QString word)
{
    int idx = -1;
    idx = html.indexOf(word);
    while(idx > -1)
    {
        html.remove(idx,word.length());
        idx = html.indexOf(word);
    }
}
void LoadFromMerriam::removeTag(QString &html, QString tag)
{
    QString keyEm = "<"+tag+">";
    removeWord(html, keyEm);

    keyEm = "</"+tag+">";
    removeWord(html, keyEm);

}

void LoadFromMerriam::parseHtml(QString &html, QString &definition)
{
    QString keyStartClass = "<span class=\"ssens\">";
    QString keyBold = "</strong>";
    QString keyEndClass = "</span>";
    int idxStarKey = -1;
    int idxEndKey = -1;

    removeLinks(html);
    removeTag(html, "em");
    idxStarKey = html.indexOf(keyStartClass);
    if(idxStarKey > -1)
    {
        idxStarKey = html.indexOf(keyBold, idxStarKey +1);
        idxEndKey  = html.indexOf(keyEndClass, idxStarKey+1);
        definition =  definition + html.mid(idxStarKey + keyBold.length(), idxEndKey - (idxStarKey + keyBold.length()));
        html.remove(0, idxStarKey+keyStartClass.length());
    }

    removeTag(definition, "strong");
    removeWord(definition, "<span class=\"vi\">&lt;");
    removeWord(definition, "aft</a>");
    removeWord(definition, "&gt");
}

void LoadFromMerriam::parsePage(bool ok)
{
    if(!ok)
    {
        qDebug()<<"nu a facut load";
        return;
    }
    else
        qDebug()<<" A facut load page la index ";

    QString data;
    QString wordDefinition;
    QString keyStartDefinition = "<span>Definition of <em>";
    data = ui->webView->page()->currentFrame ()->toHtml().toLatin1();

   /* int index_definition = data.indexOf(keyStartDefinition);
    wordDefinition       = data.mid(index_definition+keyStartDefinition.length());
    index_definition     = wordDefinition.indexOf(keyStartDefinition);    
    wordDefinition.remove( index_definition, wordDefinition.length() - index_definition );
    */
    parseHtml(data, wordDefinition);
    removeImg(wordDefinition);
    removeBackground(wordDefinition);


    QSqlField field(QString("definiton"), QVariant::String);
    field.setValue("<html>" + wordDefinition + "</html>");
    QString wordDefinition_format =  db.driver()->formatValue(field);
    formatDefinition(wordDefinition_format);

    /*
    //insert to database
    QSqlQuery query;
    QString str_insert = "insert into words(name, definition) values('" + m_wordLoad +"', " + wordDefinition_format  +" )";
    int ret = query.exec(str_insert);
    if(!ret)
        qDebug()<<"result sql = "<<ret <<query.lastError() << str_insert;
    else
        qDebug()<<" exectute succesfully";
    */
    QMap<QString,QString> word;
    word.insert("name", m_wordLoad);
    word.insert("definition", wordDefinition);

    //m_wordsDefinition.append(word);
    m_wordIndex++;

    //if(m_wordIndex < m_words.size())
    qDebug()<<m_wordIndex<<" definition="<<wordDefinition;
    //insertToXml(word);

    word.clear();
    if(m_wordIndex < 1000)
    {
       loadPage(m_wordIndex);
       ui->progressBar->setValue(m_wordIndex);
    }
    //else
      //  createXML();
}


void LoadFromMerriam::formatDefinition(QString &definiton)
{
    int index = definiton.indexOf("/");
    while(index != -1)
    {
        definiton.replace(index, 1, "//");
        index =  definiton.indexOf("/", index+2);
    }
}

void LoadFromMerriam::on_preluareBtn_clicked()
{
    initPage();
}

void LoadFromMerriam::insertToXml(QMap<QString, QString> word)
{
    if(m_xmlWriter != NULL)
    {
        qDebug()<<"insert to XML:"<<word["name"];
        m_xmlWriter->writeStartElement("word");

        m_xmlWriter->writeStartElement("name");
             m_xmlWriter->writeCharacters(word["name"]);
        m_xmlWriter->writeEndElement();

        m_xmlWriter->writeStartElement("definition");
             m_xmlWriter->writeCharacters(word["definition"]);
        m_xmlWriter->writeEndElement();
        /*close tag definition  */
        m_xmlWriter->writeEndElement();
    }
    else
        qDebug()<<"xml wite is null";
}
void LoadFromMerriam::createXML()
{       
       //QFile file("/home/florina/work/trunk/translations/words_merriam.xml");
       QFile file("/home/florina/work/Dictionary/trunk/translations/words_merriam.xml");
       /*open a file */
       if (!file.open(QIODevice::WriteOnly))
       {
           qDebug()<<"Can't open file";
       }
       else
       {
           /*if file is successfully opened then create XML*/
           QXmlStreamWriter* xmlWriter = new QXmlStreamWriter();
           /* set device (here file)to streamwriter */
           xmlWriter->setDevice(&file);
           /* Writes a document start with the XML version number version. */
           xmlWriter->writeStartDocument();
           xmlWriter->writeStartElement("words");

           for (int i = 0; i < m_wordsDefinition.size(); i++)
           {
               QMap<QString, QString> word = m_wordsDefinition.at(i);
               /* create tag person */
               xmlWriter->writeStartElement("word");
               xmlWriter->writeStartElement("name");
                    xmlWriter->writeCharacters(word["name"]);
               xmlWriter->writeEndElement();

               xmlWriter->writeStartElement("definition");
                    xmlWriter->writeCharacters(word["definition"]);
               xmlWriter->writeEndElement();
               /*close tag person  */
               xmlWriter->writeEndElement();

           }
           /*end tag persons*/
           xmlWriter->writeEndElement();
           /*end document */
           xmlWriter->writeEndDocument();
           delete xmlWriter;
       }
}


bool LoadFromMerriam::createDBConnection()
{
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("/home/florina/work/Dictionary/translations/words");
    if (!db.open()) {
        qDebug()<< tr("Unable to establish a database connection.\n");
        return false;
    }
    qDebug()<<"Load succesfully";
    return true;
}
